package com.excesys.exsecs.om.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.excesys.exsecs.common.core.util.R;
import com.excesys.exsecs.om.constans.OmCommonConstants;
import com.excesys.exsecs.om.entity.OmICheckSheet;
import com.excesys.exsecs.om.entity.OmWOrder;
import com.excesys.exsecs.om.entity.OmWOrderComplain;
import com.excesys.exsecs.om.service.*;
import com.excesys.exsecs.om.utils.DateUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 巡检计划表
 *
 * @author weiyj
 * @date 2023-03-01 14:10:58
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/count" )
@Tag(name = "统计查询")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class CountController {

	private final OmWOrderService omWOrderService;

	private final OmICheckSheetService omICheckSheetService;

	private final OmICheckPlanService omICheckPlanService;

	private final OmWOrderComplainService omWOrderComplainService;

	private final OmWEventService omWEventService;

	private final OmICheckPointService omICheckPointService;

	/**
	 * 待办数量查询
	 * @param omWOrder 工单
	 * @return
	 */
	@Operation(summary = "待办数量查询", description = "待办数量查询")
	@GetMapping("/waitcount" )
//	@PreAuthorize("@pms.hasPermission('om_omworder_get')" )
	public R getOmWOrderWaitCount(OmWOrder omWOrder) {
		Map waitcount = new HashMap<>();

//		OmWOrder omWOrder = new OmWOrder();
//		omWOrder.setHandleUserId(userId);
		omWOrder.setOrderStatus(OmCommonConstants.ORDER_STATUS_1);
		waitcount.put("orderCount",omWOrderService.list(Wrappers.query(omWOrder)).size());

		OmWOrderComplain omWOrderComplain = new OmWOrderComplain();
		omWOrderComplain.setHandleUserId(omWOrder.getHandleUserId());
		omWOrderComplain.setComplainState(OmCommonConstants.COMPLAIN_STATE_1);
		waitcount.put("complainCount",omWOrderComplainService.list(Wrappers.query(omWOrderComplain)).size());
		return R.ok(waitcount);
	}

	/**
	 * 待办数量查询
	 * @return
	 */
	@Operation(summary = "待办数量查询", description = "待办数量查询")
	@GetMapping("/todayCount" )
//	@PreAuthorize("@pms.hasPermission('om_omworder_get')" )
	public R getTodayCount() {
		QueryWrapper<OmWOrder> orderWrapper = Wrappers.<OmWOrder>query();
		String todayStartStr = DateUtils.getTodayStartTimeStamp();
		String todayEndStr = DateUtils.getTodayEndTimeStamp();
		orderWrapper.lambda().ge(OmWOrder::getApplyTime, todayStartStr);
		orderWrapper.lambda().le(OmWOrder::getApplyTime, todayEndStr);


		List<OmWOrder> orderTotalList = omWOrderService.list(orderWrapper);
		orderWrapper.lambda().ge(OmWOrder::getOrderStatus,"6");//已解决
		List<OmWOrder> orderDoneList = omWOrderService.list(orderWrapper);

		QueryWrapper<OmICheckSheet> sheetWrapper = Wrappers.<OmICheckSheet>query();
		sheetWrapper.lambda().ge(OmICheckSheet::getCreateTime, todayStartStr);
		sheetWrapper.lambda().le(OmICheckSheet::getCreateTime, todayEndStr);


		List<OmICheckSheet> sheetTotalList = omICheckSheetService.list(sheetWrapper);
		sheetWrapper.lambda().ge(OmICheckSheet::getSheetStatus,"4");//已结束
		List<OmICheckSheet> sheetDoneList = omICheckSheetService.list(sheetWrapper);

		Map count = new HashMap<>();
		count.put("orderTotalCount",orderTotalList.size());
		count.put("orderDoneCount",orderDoneList.size());
		count.put("sheetTotalCount",sheetTotalList.size());
		count.put("sheetDoneCount",sheetDoneList.size());
		return R.ok(count);
	}

	/**
	 * 待办数量查询
	 * @return
	 */
	@Operation(summary = "数量查询", description = "数量查询")
	@GetMapping("/count" )
//	@PreAuthorize("@pms.hasPermission('om_omworder_get')" )
	public R getOmWOrderCount(Long userId,Long createUserId,Long evaluateUserId) {
		Map<String,Map<String,String>> countMap = new HashMap<>();
		Map<String,String> orderMap = omWOrderService.getOrderCountMap(userId,createUserId,evaluateUserId);
		Map<String,String> sheetMap = omICheckSheetService.getSheetCountMap(userId);
		Map<String,String> eventMap = omWEventService.getEventCountMap(userId,createUserId);
		countMap.put("orderMap",orderMap);
		countMap.put("sheetMap",sheetMap);
		countMap.put("eventMap",eventMap);
		return R.ok(countMap,"首页统计查询");
	}

	/**
	 * 工单报修数量查询
	 * @return
	 */
	@Operation(summary = "报修数量查询", description = "报修数量查询")
	@GetMapping("/orderCountByTimeType" )
	public R getOmWOrderCountByTimeType(String timeType) {
		Map<String,String> orderMap = omWOrderService.getOrderCountByTimeType(timeType);
		return R.ok(orderMap,"报修数量查询");
	}



	/**
	 * 年度报修数量按月查询
	 * @return
	 */
	@Operation(summary = "年度报修数量按月查询", description = "年度报修数量按月查询")
	@GetMapping("/orderCountByYearMonth" )
	public R getOrderCountByYearMonth() {
		List<Map<String, String>> orderMap = omWOrderService.getOrderCountByYearMonth();
		return R.ok(orderMap,"年度报修数量按月查询");
	}

	/**
	 * 年度报修数量按月查询
	 * @return
	 */
	@Operation(summary = "月报修系统设备统计", description = "月报修系统设备统计")
	@GetMapping("/orderCountByDeviceType" )
	public R getOrderCountByDeviceType(String month) {
		List<Map<String, String>> orderMap = omWOrderService.getOrderCountByDeviceType(month);
		return R.ok(orderMap,"月报修系统设备统计");
	}

	/**
	 * 巡检计划数量统计
	 * @return
	 */
	@Operation(summary = "巡检计划数量统计", description = "巡检计划数量统计")
	@GetMapping("/checkPlanFinishCountByTimeType" )
	public R getCheckPlanFinishCountByTimeType(String timeType,String statusType) {
		Map<String, String> checkPlanMap = omICheckPlanService.getCheckPlanFinishCountByTimeType(timeType,statusType);
		return R.ok(checkPlanMap,"巡检计划数量统计");
	}



	/**
	 * 点位数量查询
	 * @return
	 */
	@Operation(summary = "点位数量查询", description = "点位数量查询")
	@GetMapping("/pointcount" )
//	@PreAuthorize("@pms.hasPermission('om_omworder_get')" )
	public R getPointCount() {
		Map<String,String> pointMap = omICheckPointService.getPointCountMap();
		return R.ok(pointMap,"首页统计查询");
	}




}
